package com.xgkj.diyiketang.util.xiangce;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.xgkj.diyiketang.ProtocolConst;
import com.xgkj.diyiketang.util.PalLog;
import com.xgkj.diyiketang.R;

import java.util.ArrayList;

/**
 * Created by windo on 16/4/14.
 */
public class FolderAdapter extends BaseAdapter {
    private final String TAG = getClass().getSimpleName();
    private Context mContext;
    private ArrayList<PhotoFolderInfo> mList = new ArrayList<PhotoFolderInfo>();
    private BitmapCache imageLoadCache;

    BitmapCache.ImageCallback callback = new BitmapCache.ImageCallback() {
        @Override
        public void imageLoad(ImageView imageView, Bitmap bitmap,
                              Object... params) {
            if (imageView != null && bitmap != null) {
                String url = (String) params[0];
                if (url != null && url.equals((String) imageView.getTag())) {
                    ((ImageView) imageView).setImageBitmap(bitmap);
                } else {
                    PalLog.e(TAG, "callback, bmp not match");
                }
            } else {
                PalLog.e(TAG, "callback, bmp null");
            }
        }
    };

    public FolderAdapter(Context context) {
        mContext = context;
        imageLoadCache = new BitmapCache(context);
    }

    public void updateData(ArrayList<PhotoFolderInfo> list){
        mList = list;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        if (mList != null){
            return mList.size();
        }

        return 0;
    }

    @Override
    public Object getItem(int position) {
        if (mList != null && mList.size() >0){
            return mList.get(position);
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ChildHolder childHolder = null;
        if (convertView == null || convertView.getTag() == null){
            childHolder = new ChildHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.folder_item,null);
            childHolder.folderImage = (ImageView) convertView.findViewById(R.id.folder_image);
            childHolder.mTitleText = (TextView) convertView.findViewById(R.id.folder_name);
            childHolder.mNumText = (TextView) convertView.findViewById(R.id.folder_num);
            convertView.setTag(childHolder);
        }else {
            childHolder = (ChildHolder) convertView.getTag();
        }

        final PhotoFolderInfo info = (PhotoFolderInfo) getItem(position);
        if (info != null){
            if (info.imageList!=null){
                PhotoImageInfo imageInfo = info.imageList.get(0);
                String path = imageInfo.imagePath;
                childHolder.mTitleText.setText(info.folderName);
                childHolder.mNumText.setText(String.valueOf(info.count));
                childHolder.folderImage.setTag(path);
                imageLoadCache.displayBmp(childHolder.folderImage,imageInfo.thumbnailPath,imageInfo.imagePath,callback);
//                childHolder.folderImage.setOnClickListener(new View.OnClickListener() {
//                    @Override
//                    public void onClick(View v) {
//                        Intent intent = new Intent(mContext,PhoneMainActivity.class);
//                        intent.putExtra(ProtocolConst.IMAGE_LIST,(ArrayList<PhotoImageInfo>)info.imageList);
//                        intent.putExtra(ProtocolConst.PHOTO_FROM,ProtocolConst.FROM_FOLDER);
//                        mContext.startActivity(intent);
//                    }
//                });
            }
        }
        return convertView;
    }

    class ChildHolder {
        ImageView folderImage;
        TextView mTitleText;
        TextView mNumText;

    }
}
